#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

int a = 0;
int b = 0;
pthread_mutex_t mutex_a;
pthread_mutex_t mutex_b;

void* another(void* arg) {
    pthread_mutex_lock(&mutex_b);
    cout << "in child thread, got mutext b, waiting for mutex a" << endl;
    sleep(5);
    ++b;
    pthread_mutex_lock(&mutex_a);
    b += a++;
    pthread_mutex_unlock(&mutex_a);
    pthread_mutex_unlock(&mutex_b);
    pthread_exit(nullptr);
}

int main() {
    pthread_t id;

    pthread_mutex_init(&mutex_a, nullptr);
    pthread_mutex_init(&mutex_b, nullptr);
    pthread_create(&id, nullptr, another, nullptr);

    pthread_mutex_lock(&mutex_a);
    cout << "in parent thread, got mutext a, waiting for mutex b" << endl;
    sleep(5);
    ++a;
    pthread_mutex_lock(&mutex_b);
    a += b++;
    pthread_mutex_unlock(&mutex_b);
    pthread_mutex_unlock(&mutex_a);

    pthread_join(id, nullptr);
    pthread_mutex_destroy(&mutex_a);
    pthread_mutex_destroy(&mutex_b);

    return 0;
}